<!--
 * @Author: lijing
 * @e-mail: 1413979079@qq.com
 * @LastEditors: lijing
 * @Date: 2020-04-30 21:30:04
 * @LastEditTime: 2020-06-21 13:30:12
 * @FilePath: \client\src\views\Login.vue
 * @Description: 登录界面
 -->

<template>
  <div class="major">
    <el-card>
      <div slot="header">
        <h3>欢迎登录</h3>
      </div>
      <el-form :rules="rules" :model="formData" ref="loginForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" type="password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import request from "../http/request";
import { State, Action, Mutation } from "vuex-class";
import { Route } from "vue-router";

Component.registerHooks(["beforeRouteEnter"]);

@Component
export default class Login extends Vue {
  @State token: string;
  @Mutation setToken: Function;
  @Action getUserInfo: Function;
  redirect: string;
  formData = {
    username: "",
    password: ""
  };
  rules = {
    username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }]
  };
  submitForm(formName: string) {
    const re: any = this.$refs[formName];
    re.validate((valid: any) => {
      if (valid) {
        request
          .login(this.formData)
          .then(res => {
            if (res.data.code === 0) {
              const token = res.data.data;
              this.setToken(token);
              this.getUserInfo();
              this.$router.push(this.redirect ? this.redirect : "/");
            } else {
              this.$message({
                showClose: true,
                message: "用户名或密码错误，登录失败",
                type: "error"
              });
            }
          })
          .catch(() => {
            this.$message({
              showClose: true,
              message: "服务器忙，登录失败",
              type: "error"
            });
          });
      } else {
        this.$message({
          showClose: true,
          message: "请完整填写字段",
          type: "warning"
        });
        return false;
      }
    });
  }
  resetForm(formName: string) {
    const re: any = this.$refs[formName];
    re.resetFields();
  }
  beforeRouteEnter(to: Route, from: Route, next: Function) {
    next((vm: any) => {
      const redirect: any = to.query.redirect;
      if (redirect) {
        vm.redirect = redirect;
        vm.$message({
          message: "请先登录哦",
          showClose: true,
          type: "info"
        });
      }
    });
  }
}
</script>

<style lang="scss" scoped>
.major {
  height: 100%;
  h3 {
    color: #606266;
    text-align: center;
  }
}
.el-card {
  margin: auto;
  width: 400px;
  margin-top: 80px;
}
</style>>

